package com.tx.framework.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class DateUtil {

	static DateTimeFormatter df = DateTimeFormatter
			.ofPattern("yyyy-MM-dd HH:mm:ss");

	static DateTimeFormatter dfShort = DateTimeFormatter
			.ofPattern("yyyy-MM-dd");

	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static String getCurrentTime() {
		Date date = new Date();
		return sdf.format(date);
	}

	public static String getCurrentDateTimeyyyy_MM_dd() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}

	/*
	 * public static String getCurrentDateTimeyyMMdd() { Date date = new Date();
	 * SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm"); return
	 * sdf.format(date); }
	 */

	public static LocalDateTime getLocalDateTimeAddDays(int days) {
		return LocalDateTime.now().minusDays(-days);
	}

	public static LocalDateTime getLocalDateTimeAddHours(int hours) {
		return LocalDateTime.now().minusHours(-hours);
	}

	public static LocalDateTime getLocalDateTimeAddHours(String date, int hours) {
		return LocalDateTime.parse(date, df).minusHours(-hours);
	}

	public static LocalDateTime getLocalDateTimeAddMinutes(String date,
			int minutes) {
		return LocalDateTime.parse(date, df).minusMinutes(-minutes);
	}

	public static LocalDateTime getLocalDateTimeAddHours(LocalDateTime date,
			int hours) {
		return date.minusHours(-hours);
	}

	public static LocalDateTime getLocalDateTimeAddMinutes(LocalDateTime date,
			int minutes) {
		return date.minusMinutes(-minutes);
	}

	public static LocalDateTime getLocalDateTime() {
		return LocalDateTime.now();
	}

	public static LocalDateTime getLocalDateTime(String date) {
		if (date.length() <= 10) {
			date = date + " 00:00:00";
		}
		return LocalDateTime.parse(date, df);
	}

	public static Long localDateTime2Long(LocalDateTime ldt) {
		return Timestamp.valueOf(ldt).getTime();
	}

	public static String localDateTime2String(LocalDateTime ldt) {
		return df.format(ldt);
	}

	public static String localDateTime2StringShort(LocalDateTime ldt) {
		return dfShort.format(ldt);
	}

	/**
	 * 时间戳转字符串
	 * 
	 * @param lDate
	 *            毫秒
	 * @return
	 */
	public static String long2Date(Long lDate) {
		return sdf.format(new Date(lDate));
	}

	/**
	 * date2 > date1
	 * 
	 * @param date1
	 * @param date2
	 * @return arr[0]->hour,arr[1]->minutes
	 */
	public static int[] getDiffMinutes(LocalDateTime date1, LocalDateTime date2) {
		Duration duration = Duration.between(date1, date2);
		long diffMinutes = duration.toMinutes();
		int hour = (int) diffMinutes / 60;
		int minutes = (int) diffMinutes % 60;
		int[] arr = new int[2];
		arr[0] = hour;
		arr[1] = minutes;
		return arr;
	}

	/**
	 * date2-date1
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Long getDiffDays(LocalDateTime date1, LocalDateTime date2) {
		Duration duration = Duration.between(date1, date2);
		return duration.toDays() + 1;
	}

	/**
	 * date2-date1
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Long getDiffHours(LocalDateTime date1, LocalDateTime date2) {
		Duration duration = Duration.between(date1, date2);
		return duration.toHours();
	}

	/**
	 * date2-date1
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Long getDiffMinutes2(LocalDateTime date1, LocalDateTime date2) {
		Duration duration = Duration.between(date1, date2);
		return duration.toMinutes();
	}

	/**
	 * true为日期，false为非日期格式
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isDate(String date) {
		try {
			sdf.setLenient(false);
			sdf.parse(date);
			return true;
		} catch (ParseException e) {
			return false;
		}
	}

	public static boolean isDateYYYYMMDD(String date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			format.setLenient(false);
			format.parse(date);
			return true;
		} catch (ParseException e) {
			return false;
		}
	}

	/**
	 * 检查list不为空
	 * 
	 * @param list
	 * @return
	 */
	public static boolean checkList(List<?> list) {
		if (list != null && list.size() > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 获取文件浏览URL
	 * 
	 * @param fileName
	 * @param request
	 * @return
	 */
	public static String getFileURL(String fileName, HttpServletRequest request) {
		String url = request.getHeader("X-Forwarded-Scheme") + "://"
				+ request.getServerName() + request.getContextPath()
				+ "/resources/" + fileName;
		return url;
	}

	/**
	 * 获取文件浏览基础URL
	 * 
	 * @param request
	 * @return
	 */
	public static String getFileBaseURL(HttpServletRequest request) {
		String url = request.getHeader("X-Forwarded-Scheme") + "://"
				+ request.getServerName() + request.getContextPath()
				+ "/resources/";
		return url;
	}

	/**
	 * 检查wx请求返回值是否包含errorcode，true 不包含，false 包含
	 * 
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Boolean checkResponseNoError(String response) {
		Map<String, Object> map = JSONUtil.jsonToT(response, Map.class);
		if (!map.containsKey("errcode")
				|| map.get("errcode").toString().equals("0")) {
			return true;
		}
		return false;
	}

	public static String localDateTime2yyyy_MM_dd(LocalDateTime date) {
		if (date == null) {
			return "";
		}
		return date.format(dfShort);
	}

}
